﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.Api.Dto;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class ListHealthHandler : IRequestHandler<ListHealthCommand, ApiResult<List<HealthDto>>>
    {
        public ListHealthHandler(IBaseRepository<HealthModel> healthRepository,IMapper mapper)
        {
            HealthRepository = healthRepository;
            Mapper = mapper;
        }

        public IBaseRepository<HealthModel> HealthRepository { get; }
        public IMapper Mapper { get; }

        /// <summary>
        /// 保健信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<HealthDto>>> Handle(ListHealthCommand request, CancellationToken cancellationToken)
        {
           var res=new ApiResult<List<HealthDto>>();

            List<HealthModel>? health = await HealthRepository
            .GetAll().Where(x => x.SourceId == request.SourceId).ToListAsync();
            List<HealthDto>? mapper = Mapper.Map<List<HealthDto>>(health);

            res.Data = mapper;
            res.Message = "显示保健信息成功";
            res.Code = EnumCode.Success;

            return res;
        }
    }
}
